﻿
Public Class CutusmerDAO
    Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect()

    End Sub
    Public Function GetData() As List(Of CustumersDTO)
        Dim i As Integer
        Dim cusId, fn, ln, pn, add As String
        Dim CusDTO As CustumersDTO
        Dim cusList As New List(Of CustumersDTO)
        Dim dt As New DataTable
        dt = connect.getdata("select * from custumer")
        For i = 0 To dt.Rows.Count() - 1
            cusId = dt.Rows(i)("customerId").ToString
            fn = dt.Rows(i)("fisrtname").ToString
            ln = dt.Rows(i)("lastname").ToString
            pn = dt.Rows(i)("phone").ToString
            add = dt.Rows(i)("address").ToString
            CusDTO = New CustumersDTO(cusId, fn, ln, pn, add)
            cusList.Add(CusDTO)

        Next
        Return cusList
    End Function
    Public Function InsertUpdateCustomer(ByVal cusDTO As CustumersDTO, ByVal flag As Boolean)
        Dim customerid As Int32 = cusDTO.CustomerId
        Dim fisrtname As String = cusDTO.Fisrtname
        Dim lastname As String = cusDTO.Lastname
        Dim phone As String = cusDTO.Phone
        Dim address As String = cusDTO.Address
        If flag = True Then
            sqlstr = "insert into custumer(fisrtname,lastname,phone,address) values('" & fisrtname & "','" & lastname & "','" & phone & "','" & address & "')"
        Else
            sqlstr = "update custumer set fisrtname='" & fisrtname & "',lastname='" & lastname & "',phone='" & phone & "',address='" & address & "' where customerid='" & customerid & "'"
        End If
        Return connect.Execute(sqlstr)
    End Function
    Public Function DeleteCustumer(ByVal cusDTO As CustumersDTO)
        Dim customerid As String = cusDTO.CustomerId
        sqlstr = "delete from custumer where customerid='" & customerid & "'"
        Return connect.Execute(sqlstr)
    End Function
End Class
